home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFskyedit - Star Fighter 3000 sky colours editor
- * Main application skeleton
- * Copyright (C) 2001 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- /* ANSI library files */
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <signal.h>
-
- /* RISC OS library files */
- #include "kernel.h"
- #include "wimp.h"
- #include "toolbox.h"
- #include "event.h"
- #include "wimplib.h"
- #include "flex.h"
- #include "window.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "hourglass.h"
- #include "Macros.h"
- #include "Loader.h"
- #include "Pal256.h"
- #include "ViewsMenu.h"
- #include "InputFocus.h"
-
- /* Local headers */
- #include "SFSIconbar.h"
- #include "SFSSaveBox.h"
- #include "SFSFileInfo.h"
- #include "Menus.h"
- #include "Insert.h"
- #include "Interpolate.h"
- #include "PreQuit.h"
- #include "DCS_dialogue.h"
- #include "Utils.h"
- #include "Main.h"
-
- #define WimpVersion 310
-
- /* Our toolbox events */
- #define EVENT_HELPFILE 0x03
- #define EVENT_QUIT 0x09
-
- _kernel_oserror shared_err_block = {255, ""};
- int palette[256];
- void *transtable = NULL; /* flex anchor */
- int x_eigen = 2;
- int y_eigen = 2;
- int x_windlimit = 1023;
- int y_windlimit = 767;
- int scale_factors[4] = {1,1,1,1};
- ObjectId pal256_sharedid = NULL_ObjectId;
- char taskname[32];
- bool format_warning = true;
- #ifdef INT_COLOUR_FIND
- bool use_colour_trans = false;
- #endif
- int wimp_version;
-
- static WimpPollBlock poll_block;
- static IdBlock id_block;
-
- /* ----------------------------------------------------------------------- */
- /* Function prototypes */
-
- static WimpMessageHandler wimpquit_handler, PreQuit_handler, maketranstable;
- static ToolboxEventHandler autocreate_handler, tb_quithandler, error_handler, showhelp_handler;
- static WimpEventHandler hide_nontrans_dboxes;
- static void initialise(void);
- static void process_arguments(int argc, char *argv[]);
- static void simple_exit(_kernel_oserror *e);
- static void load_cl_files(int argc, char *argv[]);
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- int main(int argc, char *argv[])
- {
- int event_code;
-
- hourglass_on();
- process_arguments(argc, argv);
- initialise();
- load_cl_files(argc, argv);
- hourglass_off();
-
- /*
- * poll loop
- */
- while (TRUE)
- event_poll (&event_code, &poll_block, 0);
- }
-
- /* ----------------------------------------------------------------------- */
- /* Private functions */
-
- static int maketranstable(WimpMessage *message, void *handle)
- {
- void *newptr;
- _kernel_swi_regs regs;
- int vduvarsblock[5];
-
- /* Find required memory for colour translation table */
- regs.r[0] = (int)13; /* source mode */
- regs.r[1] = (int)palette; /* source palette */
- regs.r[2] = -1; /* current mode */
- regs.r[3] = -1; /* current palette */
- regs.r[4] = 0; /* return length needed */
- regs.r[5] = 1; /* R1 = pointer to sprite */
- regs.r[6] = 0;
- regs.r[7] = 0;
- if(!E(_kernel_swi(ColourTrans_GenerateTable, ®s, ®s))) {
- /* Alloc required size */
- if(!flex_alloc((flex_ptr)&newptr, regs.r[4])) {
- RG("NoMem");
- }
- else {
-
- /* Read translation table */
- regs.r[4] = (int)newptr; /* ...other registers preserved */
- if(!E(_kernel_swi(ColourTrans_SelectTable, ®s, ®s))) {
-
- /* Replace current table */
- if(transtable != NULL)
- flex_free((flex_ptr)&transtable);
- flex_reanchor((flex_ptr)&transtable, (flex_ptr)&newptr);
- }
- else
- flex_free((flex_ptr)&newptr);
- }
- }
-
- /* Read eigen factors (OS-to-pixel coordinates scale) */
- vduvarsblock[0] = 4; /* XEigFactor */
- vduvarsblock[1] = 5; /* YEigFactor */
- vduvarsblock[2] = 11; /* XWindLimit */
- vduvarsblock[3] = 12; /* YWindLimit */
- vduvarsblock[4] = -1;
-
- regs.r[0] = (int)&vduvarsblock;
- regs.r[1] = (int)&vduvarsblock;
- if(!E(_kernel_swi(OS_ReadVduVariables, ®s, ®s))) {
- x_eigen = vduvarsblock[0];
- y_eigen = vduvarsblock[1];
- x_windlimit = vduvarsblock[2];
- y_windlimit = vduvarsblock[3];
- }
-
- scale_factors[0] = 2; /* x multiplication factor */
- scale_factors[1] = 2; /* y multiplication factor */
- scale_factors[2] = (1 << x_eigen); /* x division factor */
- scale_factors[3] = (1 << y_eigen); /* y division factor */
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int hide_nontrans_dboxes(int event_code, WimpPollBlock *event, IdBlock *id_block, void *handle)
- {
- /* We use some psuedo-transient dialogue boxes
- that need closing on mouse-click */
-
- if(id_block->self_id != Interpolate_sharedid
- && id_block->parent_id != Interpolate_sharedid)
- RE(hide_deiconise(Interpolate_sharedid))
-
- if(id_block->self_id != Insert_sharedid
- && id_block->parent_id != Insert_sharedid)
- RE(hide_deiconise(Insert_sharedid))
-
- return 0; /* pass this event on */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int wimpquit_handler(WimpMessage *message,void *handle)
- {
- /* Quit application immediately */
- exit(EXIT_SUCCESS);
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int tb_quithandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Quit entry on iconbar menu */
- if(TRY_QUIT(0))
- exit(EXIT_SUCCESS);
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int showhelp_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Show application help file */
- if(_kernel_oscli("Filer_Run <SFskyedit$Dir>.!Help") == _kernel_ERROR)
- err_check_rep(_kernel_last_oserror());
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int PreQuit_handler(WimpMessage *message, void *handle)
- {
- int th;
- if(message->hdr.size < 20 || message->data.words[0] == 0)
- th = message->hdr.sender; /* shutdown in progress */
- else
- th = 0; /* just our task */
-
- /* If function returns false then unsaved data */
- if(!TRY_QUIT(th)) {
-
- /* Object by acknowledging message */
- message->hdr.your_ref = message->hdr.my_ref;
- RE(wimp_send_message(Wimp_EUserMessageAcknowledge, message, message->hdr.sender, NULL, NULL))
- }
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int autocreate_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Catch auto-created objects and initialise handlers etc. */
- ToolboxObjectAutoCreatedEvent *toace = (ToolboxObjectAutoCreatedEvent *)event;
- if (strcmp(toace->template_name, "Iconbar") == 0) {
- Iconbar_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "PreQuit") == 0) {
- PreQuit_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "Menu") == 0) {
- EF(ViewsMenu_parentcreated(id_block->self_id, 0x03));
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "Interpolate") == 0) {
- Interpolate_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "Insert") == 0) {
- Insert_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "ColsMenu") == 0) {
- ColsMenu_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "SkyMenu") == 0) {
- SkyMenu_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "FileMenu") == 0) {
- FileMenu_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "EditMenu") == 0) {
- EditMenu_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "SaveFile") == 0) {
- SaveFile_initialise(id_block);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "FileInfo") == 0) {
- FileInfo_initialise(id_block);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "Pal256") == 0) {
- EF(Pal256_initialise(id_block->self_id));
- EF(event_register_toolbox_handler(id_block->self_id, Window_AboutToBeShown, InputFocus_recordcaretpos, NULL));
- pal256_sharedid = id_block->self_id; /* we only have one palette */
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "DCS") == 0) {
- DCS_initialise(id_block->self_id);
- return 1; /* claim event */
- }
-
- return 0; /* event not handled */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int error_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ToolboxErrorEvent *totee = (ToolboxErrorEvent *)event;
- if(totee->errnum == 0x80b633 || totee->errnum == 0x131c3) /* "To save drag...", locked file */
- err_report(totee->errnum, totee->errmess);
- else
- err_complain(totee->errnum, totee->errmess);
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void initialise(void)
- {
- int toolbox_events = 0,
- wimp_messages = 0;
-
- /*
- * Prevent termination on SIGINT (we use the escape key ourselves)
- */
- signal(SIGINT, SIG_IGN);
-
- /*
- * register ourselves with the Toolbox.
- */
-
- {
- _kernel_oserror *e = toolbox_initialise (0, WimpVersion, &wimp_messages, &toolbox_events, "<SFskyeditRes$Dir>",msgs_get_descriptor(), &id_block, &wimp_version, 0, 0);
- if(e != NULL)
- simple_exit(e);
- }
-
- strncpy(taskname, msgs_lookup("_TaskName"), sizeof(taskname)-1);
- err_set_taskname(taskname, (wimp_version >= 321));
-
- /*
- * initialise the flex library
- */
-
- flex_init(taskname, (int *)msgs_get_descriptor(), 0); /* (use Wimpslot and own messages file) */
- flex_set_budge(1); /* allow budging of flex when heap extends */
-
- /*
- * initialise the event library.
- */
-
- event_initialise (&id_block);
- event_set_mask (Wimp_Poll_NullMask |
- Wimp_Poll_KeyPressedMask); /* Dealt with by Toolbox */
-
- EF(event_register_toolbox_handler(-1, Toolbox_ObjectAutoCreated, autocreate_handler, NULL));
- EF(event_register_toolbox_handler(-1, Toolbox_Error, error_handler, NULL));
- EF(event_register_toolbox_handler(-1, EVENT_HELPFILE, showhelp_handler, NULL));
- EF(event_register_toolbox_handler(-1, EVENT_QUIT, tb_quithandler, NULL));
-
- EF(event_register_message_handler(Wimp_MPreQuit, PreQuit_handler, NULL));
- EF(event_register_message_handler(Wimp_MQuit, wimpquit_handler, NULL));
- EF(event_register_message_handler(Wimp_MPaletteChange, maketranstable, NULL));
- EF(event_register_message_handler(Wimp_MModeChange, maketranstable, NULL));
- EF(event_register_wimp_handler(-1, Wimp_EMouseClick, hide_nontrans_dboxes, NULL));
-
- EF(InputFocus_initialise());
-
- /* Initialise loader */
- EF(loader_initialise(0));
-
- /* Read the default mode 13 palette */
- {
- _kernel_swi_regs regs;
- regs.r[0] = 13; /* mode 13 */
- regs.r[1] = 0; /* read default palette */
- regs.r[2] = (int)palette; /* pointer to buffer */
- regs.r[3] = sizeof(palette); /* size of buffer */
- regs.r[4] = 0; /* flags */
- EF(_kernel_swi(ColourTrans_ReadPalette, ®s, ®s));
- }
-
- /* Set up translation table to plot mode gfx in current desktop mode */
- maketranstable(NULL, NULL);
-
- EF(ViewsMenu_create());
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void simple_exit(_kernel_oserror *e)
- {
- /* Limited amount we can do with no messages file... */
- wimp_report_error(e, Wimp_ReportError_Cancel, "SFskyedit");
- exit(EXIT_FAILURE);
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void process_arguments(int argc, char *argv[])
- {
- /*
- * Look at command-line parameters
- */
-
- for(int i = 1; i < argc; i++) {
- if(ViewsMenu_strcmp_nc(argv[i], "-nowarn"))
- format_warning = false;
- else {
- #ifdef INT_COLOUR_FIND
- if(ViewsMenu_strcmp_nc(argv[i], "-usect"))
- use_colour_trans = true;
- else {
- #endif
- if (*argv[i] == '-') {
- strcpy(shared_err_block.errmess, "Bad command line parameters");
- simple_exit(&shared_err_block);
- }
- #ifdef INT_COLOUR_FIND
- }
- #endif
- }
- }
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void load_cl_files(int argc, char *argv[])
- {
- /*
- * Load any files specified as command-line arguments
- */
-
- for(int i =1; i < argc; i++) {
- /* anything without a '-' in front is interpreted as a file to load */
- if (*argv[i] != '-') {
-
- /* get filetype */
- _kernel_swi_regs regs;
- regs.r[0]=23;
- regs.r[1]=(int)argv[i];
- if(!E(_kernel_swi(OS_File, ®s, ®s))) {
- if(regs.r[6] == FILETYPE_SKYCOLS) {
- void *cl_buffer;
- if(!E(load_compressed(argv[i], (flex_ptr)&cl_buffer)))
- Iconbar_openfile(argv[i], 1, (flex_ptr)&cl_buffer, regs.r[6], NULL);
- }
- }
- } /* parameter is filename */
- } /* next parameter */
- }
-
-